﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using TaskManagement.ModelLayer;

namespace DataAccess
{
    public class DBWorkflow : IDBWorkflow
    {
        private TaskContext context;

        public DBWorkflow()
        {
            context = new TaskContext();
            context.Configuration.LazyLoadingEnabled = false;
        }

        public List<Workflow> SearchWorkflows(Workflow searchWorkflow)
        {
            List<Workflow> result = null;
            try
            {
                var query = from w in context.WorkflowEntitySet
                            .Include("TaskEntitys.EmployeeEntity").Include("TaskEntitys.AccountEntity")
                            .Include("TaskEntitys.TagEntity").Include("TaskEntitys.DeliveryEntity")
                            select w;

                if (searchWorkflow.Id != 0)
                    query = query.Where(w => w.Id == searchWorkflow.Id);

                if (!string.IsNullOrWhiteSpace(searchWorkflow.Name))
                    query = query.Where(w => w.Name.Contains(searchWorkflow.Name));

                if (!string.IsNullOrWhiteSpace(searchWorkflow.Description))
                    query = query.Where(w => w.Description.Contains(searchWorkflow.Description));

                if (searchWorkflow.StartDate != null)
                    query = query.Where(w => w.StartDate == searchWorkflow.StartDate);

                result = Converter.ConvertWorkflowList(query.ToList(), true);

            }
            catch (Exception e)
            {
                ErrorLog.LogEvent("TaskManagement", "TaskManagement log", e.Message, 103, EventLogEntryType.Error);
            }

            return result;

        }


        public int CreateWorkflow(Workflow w)
        {
            int result = 0;

            try
            {
                WorkflowEntity we = Converter.ConvertWorkflowEntity(w);
                context.WorkflowEntitySet.Add(we);
                context.SaveChanges();
                result = we.Id;
            }
            catch (Exception e)
            {
                ErrorLog.LogEvent("TaskManagement", "TaskManagement log", e.Message, 106, EventLogEntryType.Error);
            }

            return result;
        }

        public bool CheckTaskDates(Workflow workflow)
        {
            var query = from w in context.WorkflowEntitySet where w.TaskEntitys.Any(t => t.Date < workflow.StartDate) select true;
            return  query.FirstOrDefault();
                       
        }

        public int UpdateWorkflow(Workflow w)
        {
            int ret = 0;
            try
            {
                WorkflowEntity we = Converter.ConvertWorkflowEntity(w);
                context.WorkflowEntitySet.Attach(we);
                context.Entry(we).State = System.Data.EntityState.Modified;
                context.SaveChanges();
                ret = 1;

            }
            catch (Exception e)
            {
                ErrorLog.LogEvent("TaskManagement", "TaskManagement log", e.Message, 107, EventLogEntryType.Error);
            }
            return ret;

        }


        public int DeleteWorkflow(IList<Workflow> workflows)
        {
            int ret = 0;
            try
            {

            foreach (Workflow w in  workflows)
            {
                WorkflowEntity we = Converter.ConvertWorkflowEntity(w);
                context.WorkflowEntitySet.Attach(we);
                context.WorkflowEntitySet.Remove(we);
            }

            context.SaveChanges();
            ret = 1;
            }
            catch (Exception e)
            {
                ErrorLog.LogEvent("TaskManagement", "TaskManagement log", e.Message, 108, EventLogEntryType.Error);
            }
            return ret;
        }
    }
}
